
	panel.ajax = function(p) {
		
		var success = function(r, o) {
			
			var script, scripts, response, uuid;
			
			uuid = "AJAX:ONCOMPLETE:" + Ext.id(); 
			panel.console.time.log(uuid, ">>>");
			response = r.responseText;
			
			if( (/(ecma|java)script/).test( r.getResponseHeader('Content-Type') ) ) {
			
				scripts = response;
				
			} else {
				
				scripts = [];
				
				var regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;
				
				while( (script = regexp.exec(response)) ) {
					scripts.push(script[1]); }
					
				scripts = scripts.join('\n');
				
			}	// if
		
			if (scripts) (window.execScript) ? window.execScript(scripts) : window.setTimeout(scripts, 0);
			
			if( (/json/).test( r.getResponseHeader('Content-Type') ) ) {

				if( p.onComplete instanceof Function ) {
					
					panel.console.time.log(
						uuid + ":JSON", 
						[ "<- JSON Length: ", r.responseText.length ].join("")
					);
					
					try {
					
						var json = Ext.util.JSON.decode(r.responseText);
						
					} catch( e ) {
						
						if( p.onFailure instanceof Function ) {
							p.onFailure(); }
						
						Ext.MessageBox.show({
					
							title: "Json Parsing Error, given data:",
							
							width: 500,
							msg: [
								
								"<div class='w_max h_500 fnt_mono' style='overflow-y: scroll; white-space:pre;'>",
								r.responseText, 
								"</div>"
							
							].join(""),
														
							buttons : Ext.MessageBox.OK,
							modal   : true

						});
						
						return false;
						
					}	// try
					
					panel.console.time.log(uuid + ":JSON", "<= JSON Parsing complete");
					
					p.onComplete(json);					
				
				}	// if

			} else if( p.onComplete instanceof Function ) {
			
				if( p.onComplete instanceof Function ) {
					
					// (!) Delayed execution
					window.setTimeout( function() { 
					
						p.onComplete(response); 
						
					}, 20); 
					
				}	// if
			
			}	// if
			
			panel.console.time.log(uuid, "<<<");
			
		};	// function
		
		var failure = function(r, o) {
			
			var onFailure = function(r, o) {
			
				if( r.responseText ) { 
				
					var message = r.responseText;
					
					try {
						
						var err = Ext.util.JSON.decode(r.responseText);
						message = err.error.message;
						
					} catch( e ) {}
				
					Ext.MessageBox.show({
					
						title: "Ajax Error",
						
						width: 500,
						msg: [
							
							"<div class='w_max h_500 fnt_mono' style='overflow-y: scroll; white-space:pre;'>",
							message, 
							"</div>"
						
						].join(""),
													
						buttons : Ext.MessageBox.OK,
						modal   : true,
						fn      : function() {
							
							if( r.status == 403 ) {
								window.location.href = "/"; }
							
						}
						
					});
					
					if( typeof(o.params._pd) != "undefined" ) {
						
						try {
							
							panel.console.log(
								"AJAX _PD DATA",
								Ext.ux.util.base64.decode(o.params._pd)
							);
							
						} catch(e) {}
						
					}	// if
						
				
				}	// if
				
			};	// function
			
			if( p.onFailure instanceof Function ) {
				
				if( p.onFailure(r, o) === false ) {
					onFailure(r, o); }
				
			} else {
				
				onFailure(r, o);
				
			}	// if
			
		};		
	
		var r = {
			
			_rt : p._rt,
			_p  : p._p,
			_pa : p._pa,
			_pd : p._pd,
			
			getRequestString: function() {

				var _pd = (
					(this._pd != undefined) ? 
						Ext.ux.util.base64.encode( Ext.util.JSON.encode( this._pd ) ) :
						undefined
				);
				
				return [
				
					"?",
					"&_rt=", this._rt,
					"&_p=",  this._p,
					"&_pa=", this._pa,
					( _pd != undefined ? "&_pd=" : "" ),
					( _pd != undefined ? _pd : "" )
					
				].join("");
				
			},
			
			request: function() {
				
				if( p.onRequest instanceof Function ) {
					p.onRequest(); }
				
				var _pd = (
					(this._pd != undefined) ? 
						Ext.ux.util.base64.encode( Ext.util.JSON.encode( this._pd ) ) :
						undefined
				);
					
				/* {% if $environment.state == "local" %} */
				panel.console.log("AJAX PD", this._pd);
				/* {% /if %} */
				
									
				var r = Ext.Ajax.request({
					
					url    : "?",
					method : "POST",
					params : {
						_rt : this._rt,
						_p  : this._p,
						_pa : this._pa,
						_pd : _pd
					},
					
					success : success,
					failure : failure
					
				});
				
				return this;
				
			}	// Function
			
		};	// r
		
		return r;	
	
	}	// Function
	